package com.tensquare.base.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.base.dao.CityDao;
import com.tensquare.base.pojo.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.IdWorker;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class CityService {
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private CityDao cityDao;
    public void save(City city) {
        city.setId(idWorker.nextId()+"");
        cityDao.insert(city);
    }

    public List<City> findAll() {
        return cityDao.selectList(null);

    }

    public City findById(String cityId) {
        return cityDao.selectById(cityId);
    }

    public void updateByCityId(City city) {
        cityDao.updateById(city);
    }

    public void deleteByCityId(String cityId) {
        cityDao.deleteById(cityId);
    }

    public Page<City> findByPage(Map<String, Object> map, Integer page, Integer size) {
        EntityWrapper<City> wrapper=new EntityWrapper<>();
        Set<String> set=map.keySet();
        for (String s : set) {
            wrapper.eq(map.get(s)!=null,s,map.get(s));
        }
        Page<City> page1=new Page<>(page,size);
        List<City> citys = cityDao.selectPage(page1, wrapper);
        page1.setRecords(citys);
        return page1;
    }

    public List<City> findBySearch(Map<String, Object> map) {
        return cityDao.selectByMap(map);
    }
}
